package com.whfc.ms.api.fim.controller;

import com.whfc.common.base.BaseController;
import com.whfc.common.result.ListData;
import com.whfc.common.result.PageData;
import com.whfc.common.result.Result;
import com.whfc.common.result.ResultUtil;
import com.whfc.common.util.SessionAttr;
import com.whfc.fim.dto.FimWarnRuleDTO;
import com.whfc.fim.param.FimSetUserParam;
import com.whfc.fuum.dto.SysAlgorithmTypeDTO;
import com.whfc.fuum.entity.SysUser;
import com.whfc.fuum.param.SysDeptAlgorithmAddParam;
import com.whfc.ms.api.fim.service.MsFimWarnRuleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @DESCRIPTION
 * @AUTHOR GuoDong_Sun
 * @DATE 2020/3/26
 */
@RestController
@RequestMapping("/ms/api/fim/rule")
public class AppFimWarnRuleController extends BaseController {
    @Autowired
    private MsFimWarnRuleService msFimWarnRuleService;

    /**
     * 智能监控规则列表
     *
     * @param user
     * @param deptId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/list", params = {"pageNum", "pageSize"})
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId,
                       @RequestParam("pageNum") Integer pageNum,
                       @RequestParam("pageSize") Integer pageSize) {
        logger.info("智能监控规则列表，loginer:{},deptId:{},pageNum:{},pageSize:{}",
                user.getUsername(), deptId, pageNum, pageSize);
        PageData<FimWarnRuleDTO> pageData = msFimWarnRuleService.list(deptId, pageNum, pageSize);
        return ResultUtil.success(pageData);
    }

    @GetMapping("list")
    public Result list(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                       @RequestParam("deptId") Integer deptId) {
        logger.info("获取所有的智能监控规则，loginer:{},deptId:{}", user.getUsername(), deptId);
        ListData<FimWarnRuleDTO> listData = msFimWarnRuleService.list(deptId);
        return ResultUtil.success(listData);
    }


    /**
     * 设置智能监控结果的消息通知人
     *
     * @param user
     * @param request
     * @return
     */
    @PostMapping("/setUser")
    public Result setUser(@SessionAttribute(SessionAttr.MS_USER) SysUser user,
                          @RequestBody @Validated FimSetUserParam request) {
        logger.info("设置智能监控结果的消息通知人，loginer:{},param:{}", user.getUsername(), request.toString());
        msFimWarnRuleService.setUser(request);
        return ResultUtil.success();
    }


    /**
     * 获取组织机构已配置的算法类型列表
     * 去重显示
     */
    @Deprecated
    @GetMapping("confList")
    public Result getAlgorithmTypelist(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                                       @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize,
                                       @RequestParam(value = "deptId", required = true) Integer deptId) {
        logger.info("算法配置：获取组织机构已配置的算法类型列表，sysUser:{},deptId:{},pageNum:{},pageSize:{}", sysUser.toString(), deptId, pageNum, pageSize);
        PageData<SysAlgorithmTypeDTO> list = msFimWarnRuleService.getAlgorithmConfList(deptId, pageNum, pageSize);
        return ResultUtil.success(list);
    }

    /**
     * 获取组织机构未配置的算法类型列表
     * 去重显示
     */
    @Deprecated
    @GetMapping("unconfList")
    public Result getUnAlgorithmTypelist(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                                         @RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize,
                                         @RequestParam(value = "deptId") Integer deptId) {
        logger.info("算法配置：获取组织机构未配置的算法类型列表，sysUser:{},deptId:{},pageNum:{},pageSize:{}", sysUser.toString(), deptId, pageNum, pageSize);
        PageData<SysAlgorithmTypeDTO> list = msFimWarnRuleService.getAlgorithmUnConfList(deptId, pageNum, pageSize);
        return ResultUtil.success(list);
    }

    /**
     * 添加配置组织机构算法类型
     */
    @Deprecated
    @PostMapping("addconf")
    public Result getAlgorithmTypelist(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                                       @RequestBody @Validated SysDeptAlgorithmAddParam request) {
        logger.info("算法配置：组织机构配置算法类型，sysUser:{},request:{}", sysUser.toString(), request.toString());
        msFimWarnRuleService.addAlgorithmConf(request.getDeptId(), request.getTypes());
        return ResultUtil.success();
    }

    /**
     * 删除组织机构算法类型
     */
    @Deprecated
    @PostMapping("delconf/{deptId}/{type}")
    public Result getAlgorithmTypelist(@SessionAttribute(SessionAttr.MS_USER) SysUser sysUser,
                                       @PathVariable("deptId") Integer deptId,
                                       @PathVariable("type") Integer type) {
        logger.info("算法配置：组织机构删除配置算法类型，sysUser:{},deptId:{},type:{}", sysUser.toString(), deptId, type);
        msFimWarnRuleService.delectAlgorithmConf(deptId, type);
        return ResultUtil.success();
    }

}
